import os
import re
from Problem.question import Question
import json
import sys
from Tools.tools import flat_list
from time import sleep

data: list = []
QUESTION_POSITION = "./public/data.txt"

with open(os.path.join(os.getcwd(), QUESTION_POSITION), 'r', encoding='utf-8') as fp:
    try:
        data = json.loads(fp.read())
    except Exception as e:
        print(e)

data = flat_list(data)


def find(s: str):
    if s == None or s == "":
        return
    for question in data:
        result = re.search(s, question['title'])
        if result == None:
            continue
        ques = Question(question.get("title"), question.get(
            "right"), question.get("options"))
        ques.print_ques_with_answer()
        print("\n")


# find('行数据连接时使用的默认端口号')

while True:
    try:
        find_str = input("请输入题目中的关键词:\n")
        find(find_str)
        sleep(1)
        print("\n\n")
    except KeyboardInterrupt:
        exit(0)
